﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Solution.WebsiteSolutionItem;
using BCDto.Common.Solution.WebsiteSolutionItem;
using BCEntity.Common.Solution.WebsiteSolutionItem;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.Common.Solution.WebsiteSolution;
using BCDto.Common.Solution.WebsiteSolution;
using BCDto.Sim.Admin.Admin;

namespace BCService.Common.Solution.WebsiteSolutionItem
{
    public class WebsiteSolutionItemService : IWebsiteSolutionItemService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteSolutionData websiteSolutionData;
        private readonly IWebsiteSolutionItemData websiteSolutionItemData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteSolutionItemEntity);

        public WebsiteSolutionItemService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteSolutionData websiteSolutionData,
            IWebsiteSolutionItemData websiteSolutionItemData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteSolutionData = websiteSolutionData;
            this.websiteSolutionItemData = websiteSolutionItemData;
        }

        #region 检查官网解决方案项目是否存在
        public bool Exists(int websiteSolutionItemId)
        {
            return this.websiteSolutionItemData.Exists(websiteSolutionItemId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网解决方案项目
        public WebsiteSolutionItemDto Add(WebsiteSolutionItemRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<WebsiteSolutionItemEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            return this.websiteSolutionItemData.Add(entity).GetAwaiter().GetResult().As<WebsiteSolutionItemDto>();
        }
        #endregion

        #region 修改官网解决方案项目
        public WebsiteSolutionItemDto Update(WebsiteSolutionItemPutDto putDto,AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteSolutionItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WebsiteSolutionItemEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteSolutionItemData.Update(entity).GetAwaiter().GetResult().As<WebsiteSolutionItemDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteSolutionItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网解决方案项目
        public bool Delete(int websiteSolutionItemId)
        {
            if (!this.Exists(websiteSolutionItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteSolutionItemData.Delete(websiteSolutionItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteSolutionItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网解决方案项目
        public WebsiteSolutionItemDto Get(int websiteSolutionItemId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteSolutionItemId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteSolutionItemData.GetEntity(websiteSolutionItemId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteSolutionItemDto>();
        }
        #endregion

        #region 获取官网解决方案项目列表
        public IEnumerable<WebsiteSolutionItemDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                var solutionList = this.websiteSolutionData.GetList().Result;
                var viewDtos = solutionList.As<IEnumerable<WebsiteSolutionViewDto>>();
                foreach (var item in viewDtos)
                {
                    item.Items = this.websiteSolutionItemData.GetList(item.WebsiteSolutionId).Result.As<List<WebsiteSolutionItemViewDto>>();
                }
                return viewDtos;
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteSolutionItemDto>>();
        }
        #endregion

        #region 获取官网解决方案项目分页
        public PageableList<WebsiteSolutionItemDto> Query(string solutionName, string solutionItemName, bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, solutionName, solutionItemName, enabled, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteSolutionItemData.Query(solutionName, solutionItemName, enabled, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteSolutionItemDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteSolutionItemDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网解决方案项目
        public bool Enabled(int websiteSolutionItemId)
        {
            if (!this.Exists(websiteSolutionItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteSolutionItemData.Enabled(websiteSolutionItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteSolutionItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网解决方案项目
        public bool Disabled(int websiteSolutionItemId)
        {
            if (!this.Exists(websiteSolutionItemId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteSolutionItemData.Disabled(websiteSolutionItemId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteSolutionItemId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}